package com.ruoyi.web.utils;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import lombok.Data;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * POI_Excel 自定义示例导出
 */
public class ExampleExcelExport {

    private static final String ExamplePath = "D://ExampleExcelExport//";

    public static void main(String[] args) {

        // 模拟创建实体类
        ExampleExcelExportVo exportVo = new ExampleExcelExportVo();
        {
            List<ExampleExcelExportVo.Detail> detailList = new ArrayList<>();

            ExampleExcelExportVo.Detail detail1 = new ExampleExcelExportVo.Detail();
            detail1.setFigureNumber("图号001");
            detail1.setTotalQuantity(BigDecimal.valueOf(15));
            detail1.setCtnQuantity(BigDecimal.valueOf(5));
            detail1.setCtnNw(BigDecimal.valueOf(4));
            detail1.setCtnGw(BigDecimal.valueOf(4));
            detail1.setTotalCtn(BigDecimal.valueOf(3));
            detail1.setTotalGw(BigDecimal.valueOf(12));
            detail1.setPackageSize("100x100x100");
            detail1.setPackageKind("纸箱");
            detailList.add(detail1);

            ExampleExcelExportVo.Detail detail2 = new ExampleExcelExportVo.Detail();
            detail2.setFigureNumber("图号002");
            detail2.setTotalQuantity(BigDecimal.valueOf(5));
            detail2.setCtnQuantity(BigDecimal.valueOf(5));
            detail2.setCtnNw(BigDecimal.valueOf(4));
            detail2.setCtnGw(BigDecimal.valueOf(4));
            detail2.setTotalCtn(BigDecimal.valueOf(1));
            detail2.setTotalGw(BigDecimal.valueOf(4));
            detail2.setPackageSize("110x100x70");
            detail2.setPackageKind("纸箱");
            detailList.add(detail2);

            exportVo.setDeliverGoodsDate(new Date());
            exportVo.setDeliverGoodsName("GGNB");
            exportVo.setReceiveGoodsName("Treks");
            exportVo.setReceiveGoodsAddress("雪松路人才公寓");
            exportVo.setTotalCtn(BigDecimal.valueOf(20));
            exportVo.setTotalGw(BigDecimal.valueOf(16));
            exportVo.setContractNumber("HT2024100914460001");
            exportVo.setDetailList(detailList);

        }

        export(exportVo, null);
    }

    private static void export(ExampleExcelExportVo exportVo, HttpServletResponse response) {

        //1、创建一个工作簿
        Workbook workbook = new XSSFWorkbook();
        //2、创建一个工作表
        Sheet sheet = workbook.createSheet("sheet1");

        // 第一行内容
        Row row1 = sheet.createRow(0);
        row1.createCell(0).setCellValue("生产厂：xxxx有限公司");
        row1.createCell(4).setCellValue("合同编号：" + exportVo.getContractNumber());
        row1.createCell(8).setCellValue(DateUtil.format(exportVo.getDeliverGoodsDate(), DatePattern.NORM_DATE_PATTERN));
        // 第一行合并单元格
        POIExcelUtil.setMergedRegion(sheet, 0, 0, 0, 2);
        POIExcelUtil.setMergedRegion(sheet, 0, 0, 4, 6);
        POIExcelUtil.setMergedRegion(sheet, 0, 0, 8, 9);

        // 第二行表头
        String[] titel = {"编号","产品图号","数量\r\n(件)","单箱数量\r\n(件)","单箱净重\r\n(件)",
                "单箱毛重\r\n(件)","合计箱数\r\n(箱)","合计重量\r\n(Kg)","包装尺寸\r\n(mm)","包装种类"};
        Row row2 = sheet.createRow(1);
        for (int i = 0; i < titel.length; i++) {
            row2.createCell(i).setCellValue(titel[i]);
        }

        // 写入数据
        int rowNum = 2;
        for (int i = 0; i < exportVo.getDetailList().size(); i++) {
            ExampleExcelExportVo.Detail detail = exportVo.getDetailList().get(i);
            Row rowi = sheet.createRow(rowNum);
            rowi.createCell(0).setCellValue(i+1);
            rowi.createCell(1).setCellValue(detail.getFigureNumber());
            rowi.createCell(2).setCellValue(detail.getTotalQuantity().toString());
            rowi.createCell(3).setCellValue(detail.getCtnQuantity().toString());
            rowi.createCell(4).setCellValue(detail.getCtnNw().toString());
            rowi.createCell(5).setCellValue(detail.getCtnGw().toString());
            rowi.createCell(6).setCellValue(detail.getTotalCtn().toString());
            rowi.createCell(7).setCellValue(detail.getTotalGw().toString());
            rowi.createCell(8).setCellValue(detail.getPackageSize());
            rowi.createCell(9).setCellValue(detail.getPackageKind());
            rowNum ++;
        }

        // 合并计数行
        Row row = sheet.createRow(rowNum);
        row.createCell(0).setCellValue("合计");
        row.createCell(6).setCellValue(exportVo.getTotalCtn().toString());
        row.createCell(7).setCellValue(exportVo.getTotalGw().toString());
        rowNum++;

        // 最后一行
        Row rowLast = sheet.createRow(rowNum);
        rowLast.createCell(0).setCellValue("交货人：" + exportVo.getDeliverGoodsName());
        rowLast.createCell(4).setCellValue("发往地点：" + exportVo.getReceiveGoodsAddress());
        rowLast.createCell(8).setCellValue("收货人：" + exportVo.getReceiveGoodsName());
        rowNum++;

        // 不足50行，添加空白行
        for (int i = 0; i < 50 - rowNum; i++) {
            sheet.createRow(rowNum + i);
        }


        //设置自适应列宽
        POIExcelUtil.autoSizeColumn(sheet, 0, titel.length - 1);
        // 样式
        CellStyle style = POIExcelUtil.getCellStyle(workbook);
        POIExcelUtil.standardCellStyle1(style);
        POIExcelUtil.standardCellStyle2(style);
        POIExcelUtil.standardCellStyle3(style, workbook);
        POIExcelUtil.setStyleArea(sheet, style, 1, rowNum - 2, 0 ,titel.length - 1);

        // 导出至前端
//        POIExcelUtil.exportExcel(workbook, response);

        // 导出至本地
        try {
            File file = new File(ExamplePath);
            if (!file.exists()) {
                file.mkdirs();// 创建文件根目录
            }
            String savePath = ExamplePath + System.currentTimeMillis() + ".xlsx";
            OutputStream os = Files.newOutputStream(Paths.get(savePath));
            workbook.write(os);
            os.flush();
            os.close();
            System.out.println("示例导出完毕：" + savePath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Data
    private static class ExampleExcelExportVo
    {

        /** 发货日期 */
        private Date deliverGoodsDate;

        /** 交货人 */
        private String deliverGoodsName;

        /** 收货人 */
        private String receiveGoodsName;

        /** 收货地 */
        private String receiveGoodsAddress;

        /** 总箱数 */
        private BigDecimal totalCtn;

        /** 总毛重 */
        private BigDecimal totalGw;

        /** 合同编号 */
        private String contractNumber;

        /** 发货单详情 */
        private List<Detail> detailList;

        @Data
        private static class Detail
        {

            /** 图号 */
            private String figureNumber;

            /** 总数量 */
            private BigDecimal totalQuantity;

            /** 每箱数量 */
            private BigDecimal ctnQuantity;

            /** 每箱净重 */
            private BigDecimal ctnNw;

            /** 每箱毛重 */
            private BigDecimal ctnGw;

            /** 合计箱数 */
            private BigDecimal totalCtn;

            /** 总毛重 */
            private BigDecimal totalGw;

            /** 包装尺寸 */
            private String packageSize;

            /** 包装种类 */
            private String packageKind;

        }

    }

}
